#!/usr/bin/env node

const http = require('http'),
      {join} = require('path'),
      fs = require('fs');

const buffer = {};

http.createServer((req,res)=>{
  console.log(req.url);
  console.time('MD');
  let fileName = req.url.substring(1);
  fileName = join(__dirname,fileName); 
  let content;
  try{
    if(fileName in buffer){
      //console.log('in buffer')
      content = buffer[fileName];
    }else{
      content = fs.readFileSync(fileName,'utf8');
      buffer[fileName] = content;
    }
}catch(e){
    content = e.message;
  }
  console.timeEnd('MD');
  res.end(content);
}).listen(8080);

//两种测试方法：timeend看时间，in buffer标记

